package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Title: DishServiceImpl
 * @Package com.itheima.reggie.service.impl
 * @Author: 🐟
 * @Copyright 🐟
 * @CreateTime: 2022/9/12 11:36
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品，同时保存对应的口味数据
     *
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getwithflavor(String id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(lqw);
        dishDto.setFlavors(list);
        return dishDto;
    }

    //更新菜品
    @Override
    public void updatedish(DishDto dishDto) {
        String key = "Category_" + dishDto.getCategoryId() + "_status_1";
        redisTemplate.delete(key);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        this.updateById(dish);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorService.remove(lqw);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setName(flavor.getName());
            dishFlavor.setValue(flavor.getValue());
            dishFlavor.setDishId(dish.getId());
            dishFlavorService.save(dishFlavor);
        }
    }

    @Override
    public void updatestatus(List<Long> ids) {
        LambdaUpdateWrapper<Dish> qw = new LambdaUpdateWrapper<>();
        qw.set(Dish::getStatus, 1);// set status = ?
        qw.in(Dish::getId, ids);//  where id in (1,2,3)
        this.update(qw);

    }

    @Override
    public void updatestatus2(List<Long> ids) {
        LambdaUpdateWrapper<Dish> qw = new LambdaUpdateWrapper<>();
        qw.set(Dish::getStatus, 0);// set status = ?
        qw.in(Dish::getId, ids);//  where id in (1,2,3)
        this.remove(qw);
    }

    @Override
    public void delectbyids(List<Long> ids) {
        LambdaUpdateWrapper<Dish> qw = new LambdaUpdateWrapper<>();
        qw.set(Dish::getDeleted, 1);
        qw.in(Dish::getId, ids);//  where id in (1,2,3)
        this.remove(qw);
    }
}
